package com.py.framework.core.rest.support;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.py.framework.core.utils.JsonSerializerUtils;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class RestResultNullInterceptor extends HandlerInterceptorAdapter {

	private static String nullResultJson = null;

	static {
		try {
			nullResultJson = JsonSerializerUtils.serializeToText(new RestServiceResult(null));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) throws Exception {
		if (response.getContentType() == null && null == request.getAttribute("barcode")) {
			response.setContentType("application/json;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter pw = response.getWriter();
			pw.print(nullResultJson);
			pw.flush();
			pw.close();
		}
	}

}
